<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
	<meta charset="UTF-8">
	<title>登录</title>
	<link rel="stylesheet" type="text/css" href="css/public.css"/>
	<link rel="stylesheet" type="text/css" href="css/login.css"/>
	<script src="X-admin/js/jquery.js"></script>
	<script src="X-admin/lib/layui/layui.js"></script>
	<link rel="stylesheet" href="X-admin/css/xadmin.css">
	<script src="js/public.js"></script>
</head>
<body>
<!-------------------login-------------------------->
<div class="login">
	<form method="post">
		<h1><a href="index.html"><img src="img/logo1.png" style="height: 80px;width: 160px"></a></h1>
		<p></p>
		<div class="msg-warn hide"><b></b>公共场所不建议自动登录，以防账号丢失</div>
		<p><input type="text" id="username" name="" value="666@qq.com" placeholder="昵称/邮箱/手机号"></p>
		<p><input type="text" id="passwd" name="" value="123456" placeholder="密码"></p>
		<div class="code">
			<div>
				<input name="code" lay-verify="code" placeholder="验证码" type="text" class="layui-input"
					   id="code" value="0">
			</div>
			<img src="http://localhost:9200/api-person/code/getCode" id="code_img">
		</div>

		<p><input type="submit" lay-submit lay-filter="login" name="" value="登  录"></p>
		<p class="txt"><a class="" href="reg.html">免费注册</a><a href="forget.html">忘记密码？</a></p>
		<p><input type="radio" name="autoLogin" value="1">自动登录</p>
	</form>
</div>
</body>
<script>

	var COOKIE_NAME_AUTOLOGIN = "autoLogin";
	var COOKIE_NAME = "universe";
	checkRadio();  //检查单选框
	function checkRadio() {
		if (document.cookie.indexOf(COOKIE_NAME_AUTOLOGIN) != -1) {
			$("input[name='autoLogin']").attr("checked", 'checked')
		}
	}

	cookieStart(); //单选框点击切换状态
	function cookieStart() {
		var old = null; //用来保存原来的对象
		$("input[name='autoLogin']").each(function () {//循环绑定事件
			if (this.checked) {
				old = this; //如果当前对象选中，保存该对象
				console.log(old.value)
				// addCookie(COOKIE_NAME, old.value, 60)
			}
			this.onclick = function () {
				if (this == old) {//如果点击的对象原来是选中的，取消选中
					this.checked = false;
					old = null;
					DelCookie(COOKIE_NAME_AUTOLOGIN);
				} else {
					old = this;
					addCookie(COOKIE_NAME_AUTOLOGIN, old.value);
				}
			}
		});
	}

	//写Cookie-->判断是否启用自动登录
	function addCookie(objName, objValue) {
		var str = objName + "=" + objValue;  // 设置cookie的value
		// if (time > 0) {//设置cookie存活时间
		var date = new Date();
		// var ms = objHours * 3600 * 1000;
		var ms = 3600 * 1000*24*365; //设置记住自动登录状态为时间为一年
		date.setTime(date.getTime() + ms);
		str += "; expires=" + date.toGMTString();

		document.cookie = str;
	}
	//写cookie-->登录成功后设置保存用户信息的cookie
	function addCookieForUser(username, userid, avatar, time) {
		if (document.cookie.indexOf(COOKIE_NAME_AUTOLOGIN) !== -1) { //检查是否启用了自动登录
			console.log(userid);
			var str = COOKIE_NAME + "=" + username  + ":" + userid + ":" + avatar;
			if (time > 0) {//设置cookie存活时间
				var date = new Date();
				// var ms = objHours * 3600 * 1000;
				var ms = time * 3600 * 1000; //暂时设置保存用户信息cookie为7天，7天内实现自动登录
				date.setTime(date.getTime() + ms);
				str += "; expires=" + date.toGMTString();
			}
			console.log(str);
			document.cookie = str;
		}
	}

	//清除cookie,将过期时间改为-1 即可
	function DelCookie(name) {
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		document.cookie = name + "=" + 1 + "; expires=" + exp.toGMTString();
	}

	//登录开始
	$(function () {
		layui.use('form', function () {
			var form = layui.form;
			form.on('submit(login)', function () {
				let field = {
					userna: $("#username").val(),
					passwd: $("#passwd").val(),
					locode: $("#code").val()
				};
				$.ajax({
					url: 'http://localhost:9200/api-person/login/loginCheck',//地址：访问后台的地址
					data: field,//前端传递给后台的数据
					type: 'post',
					async: false,
					xhrFields: {
						withCredentials: true
					},
					crossDomain: true,
					dataType: 'json',//是后端传递到前端的数据格式
					success: function (data) {//后端给前端的一个反馈
						console.log(data)
						if (data.msg == "验证码不正确！") {
							layer.msg("验证码不正确！");
						}
						if (data.msg == "密码错误！") {
							layer.msg("密码错误！");
						}
						if (data.msg == "用户不存在或被禁用！") {
							layer.msg("骗子！你还不是会员就想登录！最好检查一下用户名！");
						}
						if (data.code == 0) {
							layer.msg("登录成功！", function () {
								sessionStorage.setItem("userid", data.data.userid);
								sessionStorage.setItem("avatar", data.data.avatar);
								sessionStorage.setItem("userType","user");
								//登录成功后，将用户信息，保存到cookie中。
								addCookieForUser(data.data.userna, data.data.userid, data.data.avatar, 7 * 24);
								console.log(data.data.userid);
								//跳转
								window.open("index.html", "_self");
							});
						}
					}
				});
				return false;
			});
		});
		//刷新验证码
		$('#code_img').click(function () {
			$("#code_img").attr('src', "http://localhost:9200/api-person/code/getCode?random=" + Math.random());
		});
	})
</script>
</html>
